
//
// 120528 - AH - (Aura) Created.
//

package org.soekrat.environment;

import java.io.CharArrayWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *   TODO
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC)
 */
public final class RequestedSemanticEffects 
  implements Iterable<RequestedSemanticEffects.RequestedSemanticEffect> 
{
  /**
   * <p>
   *   Represents a requested semantic effect.   
   * </p>
   * @author Arne Halvorsen (AH)
   * @author Laetitia Le Chatton (LLC)
   */
  public static final class RequestedSemanticEffect
  {
    private final String m_semanticName;
    
    private final Map<String, String> m_table=new HashMap<String, String>();
    
    /**
     * <p>
     *   Constructor.
     * </p>
     */
    public RequestedSemanticEffect(String semanticName)
    {
      if (semanticName==null)
      {
        throw new NullPointerException("semanticName");
      }
      
      m_semanticName=semanticName;
    }
    
    /**
     * <p>
     *   Gets name on {@code this} requested semantic effect.
     * </p>
     * @return Name.
     */
    public String getName(){ return m_semanticName; }
    
    /**
     * <p>
     *   Gets column's data.
     * </p>
     * @param name Column name.
     * @return Content or {@code null} if not found.
     */
    public String getData(String name)
    {
      if (name==null)
      {
        throw new NullPointerException("name");
      }
      
      return m_table.get(name);
    }
    
    /**
     * <p>
     *   Sets column's data.
     * </p>
     * @param name  Column name.
     * @param value Column value.
     */
    public void setData(String name, String value)
    {
      if (name==null)
      {
        throw new NullPointerException("name");
      }
      if (value==null)
      {
        throw new NullPointerException("value");
      }
      
      m_table.put(name, value);
    }
    
    @Override
    public String toString() 
    { 
      StringBuilder sb=new StringBuilder();
      sb.append("name=").append(m_semanticName);
      for (Map.Entry<String, String> e : m_table.entrySet()) 
      {
        sb.append(", ").append(e.getKey()).append("=").append(e.getValue());
      }
      return sb.toString();
    }
  }
  
  public List<RequestedSemanticEffect> m_effects=new ArrayList<RequestedSemanticEffect>();
  
  /**
   * <p>
   *   Creates
   *   {@link RequestedSemanticEffect}.
   * </p>
   * @param name Name on requested semantic effect.
   * @return Created
   */
  public RequestedSemanticEffect create(String name)
  {
    RequestedSemanticEffect retVal=new RequestedSemanticEffect(name);
    m_effects.add(retVal);
    return retVal;
  }
  
  public int size(){ return m_effects.size(); }
  
  public RequestedSemanticEffect getEffect(int idx){ return m_effects.get(idx); }

  public Iterator<RequestedSemanticEffect> iterator(){ return m_effects.iterator(); }
  
  @Override
  public String toString() 
  {
    CharArrayWriter caw=new CharArrayWriter();
    PrintWriter pw=new PrintWriter(caw);
    
    for (RequestedSemanticEffect effect : this) pw.println(effect);
    
    pw.flush();
    return caw.toString();
  }
 
}
